package com.mrd.shiptask_shipowner.unit_tools;

import java.text.DecimalFormat;

import org.json.JSONException;
import org.json.JSONObject;

import com.mrd.shiptask_shipowner.constants.HttpConstant;
import com.mrd.shiptask_shipowner.serviecs.DownAPKService;
import com.mrd.shiptask_shipowner.shiareutil.SharePreferenceUtil;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.ColorMatrixColorFilter;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Space;
import android.widget.Toast;

public class DatasTools {
	/**
	 * JSONObject 中不存在该key的值，则返回空
	 * 
	 * @param job
	 * @param key
	 * @return
	 */
	public static String getJsonValue(JSONObject job, String key) {
		try {
			if (job.has(key)) {
				return job.get(key).toString();
			} else {
				return "";
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			return "";
		}
	}

	public static int getVersionCode(Context context)// 获取版本号(内部识别号)
	{
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
			return pi.versionCode;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	public static String getVersion(Context context)// 获取版本号
	{
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0);
			return pi.versionName;
		} catch (NameNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "0.0";
		}
	}

	public static void downVersionAction(final Context context,
			final String result) {
		try {
			JSONObject joResult = new JSONObject(result);
			String size = joResult.has("size") == false ? "0.00" : joResult
					.getString("size");
			String getVersionName = joResult.has("versionName") == false ? "1.0"
					: joResult.getString("versionName");
			final String getUrl = joResult.has("url") == false ? "null"
					: joResult.getString("url");
			AlertDialog.Builder dialog = new Builder(context);
			double getSize = Double.parseDouble(size);
			getSize = getSize / 1024 / 1024;
			DecimalFormat df = new DecimalFormat("######0.00");
			dialog.setMessage("新版本：mrd v" + getVersionName + "\n更新总耗流量："
					+ df.format(getSize) + "M");
			dialog.setTitle("版本更新！");
			dialog.setPositiveButton("立即升级", new AlertDialog.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(context, DownAPKService.class);
					intent.putExtra("apk_url", getUrl);
					intent.setPackage(context.getPackageName());
					context.startService(intent);
					Toast.makeText(context, "正在后台进行下载，稍后会自动安装",
							Toast.LENGTH_SHORT).show();
					dialog.cancel();
				}
			});
			dialog.setNegativeButton("以后再说", new AlertDialog.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			});
			dialog.show();
		} catch (JSONException e) {

		} catch (Exception e) {

		}
	}

	/**
	 * 点击imagebutton的变暗效果
	 */
	public final static float[] BT_SELECTED = new float[] { 1, 0, 0, 0, -50, 0,
			1, 0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };

	public final static float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0,
			0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };
	public static final OnTouchListener sotl = new OnTouchListener() {

		@SuppressWarnings("deprecation")
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// if(event.getAction() ==
			// MotionEvent.ACTION_DOWN)
			// {
			// v.setBackgroundResource(R.drawable.login_button1);
			// } else if(event.getAction() ==
			// MotionEvent.ACTION_UP) {
			// v.setBackgroundResource(R.drawable.login_button);
			// }
			if (event.getAction() == MotionEvent.ACTION_DOWN) {

				v.getBackground().setColorFilter(

				new ColorMatrixColorFilter(BT_SELECTED));

				v.setBackgroundDrawable(v.getBackground());

			} else if (event.getAction() == MotionEvent.ACTION_UP) {

				v.getBackground().setColorFilter(

				new ColorMatrixColorFilter(BT_NOT_SELECTED));

				v.setBackgroundDrawable(v.getBackground());

			}
			return false;
		}
	};

	public static void initDatasAboutDifferenceAccount(Context ctx) {
		// TODO Auto-generated method stub
		// token
		SharePreferenceUtil spu = new SharePreferenceUtil(ctx,
				HttpConstant.SAVE_USER);
		spu.setPushToken("");
	}
}
